package com.example.card.domain.user;

import com.example.card.CardConstant;

import java.util.ArrayList;
import java.util.List;

public class User {

    private int id;
    private String name;
    private String phone;


    private final List<OilCoin> coinList = new ArrayList<>();

    private VOil vOil;

    public User(int id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public List<OilCoin> getCoinList() {
        return coinList;
    }

    public void addCoin(OilCoin oilCoin) {
        this.coinList.add(oilCoin);
    }

    public void deductCoin(int type, int number) {
        for (OilCoin oilCoin : coinList) {
            if (oilCoin.getType() == type) {
                oilCoin.deductCoin(number);
                return;
            }
        }
    }

    public VOil getvOil() {
        return vOil;
    }


    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void addVOil(int amount) {
        if (vOil == null) {
            vOil = new VOil(amount);
        } else {
            vOil.setAmount(amount);
        }
    }

    public DeductDetail deductVOil(int amount) {
        if (vOil == null) return null;
        int number = (int) (amount * 0.2f / 100);
        vOil.deductVOil(number);
        return new DeductDetail(number);
    }

    public DeductDetail deductOilCoin(int amount) {

        for (int ocType : CardConstant.OIL_COIN_DEDUCT_SORT) {
            int number = deductOilCoinNumber(ocType, amount);
            if (number > 0) {
                deductCoin(ocType, number);
                return new DeductDetail(ocType, number);
            }
        }
        return null;
    }

    private int deductOilCoinNumber(int type, int amount) {
        for (OilCoin oilCoin : coinList) {
            if (type == oilCoin.getType()) {
                Float aFloat = CardConstant.OIL_COIN_DEDUCT_MAP.get(type);
                int number = (int) (amount * aFloat / 100);
                return Math.min(number, oilCoin.getNumber());
            }
        }
        return 0;
    }
}
